
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using TrainingsApp.Model;
using TrainingsApp.Helper;
using WisDragon.Framework.Data.DataAccess;

namespace TrainingsApp.DAL {
    public class TS_TeachMaterials_Chapter_DAL {
        public static TS_TeachMaterials_Chapter_Collecion TS_TeachMaterials_Chapter_ListByPaging(int pageSize, int pageIndex) {
            TS_TeachMaterials_Chapter_Collecion list = new TS_TeachMaterials_Chapter_Collecion();
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_TeachMaterials_Chapter_ListByPaging");
                cmd.SetParameterValue("@pageSize", pageSize);
                cmd.SetParameterValue("@pageIndex", pageIndex);
                List<TS_TeachMaterials_Chapter_Entity> pageList = cmd.ExecuteEntityList<TS_TeachMaterials_Chapter_Entity>();
                list.TS_TeachMaterials_Chapter_List = pageList;
                list.TotalCount = Convert.ToInt32(cmd.GetParameterValue("@recordCount"));
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }

            return list;

        }

        public static List<TS_TeachMaterials_Chapter_Entity> TS_TeachMaterials_Chapter_ListByParent(long parentIdx, long materialsId) {
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_TeachMaterials_Chapter_ListByParent");
                cmd.SetParameterValue("@ParentIdx", parentIdx);
                cmd.SetParameterValue("@Materialsid", materialsId);
                return cmd.ExecuteEntityList<TS_TeachMaterials_Chapter_Entity>();


            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }

            return new List<TS_TeachMaterials_Chapter_Entity>(0);
        }

        public static TS_TeachMaterials_Chapter_Entity TS_TeachMaterials_Chapter_View(System.Int64 Idx) {
            TS_TeachMaterials_Chapter_Entity c = default(TS_TeachMaterials_Chapter_Entity);
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_TeachMaterials_Chapter_View");
                cmd.SetParameterValue("@Idx", Idx);
                c = cmd.ExecuteEntity<TS_TeachMaterials_Chapter_Entity>();
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }
            return c;
        }

        public static TS_TeachMaterials_Chapter_Entity TS_TeachMaterials_Chapter_Add(TS_TeachMaterials_Chapter_Entity entity) {
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_TeachMaterials_Chapter_Add2");
                cmd.SetParameterValue("@Idx", entity.Idx);
                cmd.SetParameterValue("@MaterialsId", entity.MaterialsId);
                cmd.SetParameterValue("@ParentIdx", entity.ParentIdx);
                cmd.SetParameterValue("@Name", entity.Name);
                cmd.SetParameterValue("@RootPath", entity.RootPath);
                cmd.SetParameterValue("@IsLeaf", entity.IsLeaf);
                cmd.SetParameterValue("@Code", entity.Code);
                cmd.SetParameterValue("@IsDelete", 0);
                cmd.ExecuteNonQuery();

                entity.Idx = (long)cmd.GetParameterValue("Idx");
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }

            return entity;
        }

        public static void TS_TeachMaterials_Chapter_Update(TS_TeachMaterials_Chapter_Entity entity) {
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_TeachMaterials_Chapter_Update");
                cmd.SetParameterValue("@Idx", entity.Idx);

                cmd.SetParameterValue("@Name", entity.Name);

                cmd.SetParameterValue("@Code", entity.Code);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }
        }

        public static void TS_TeachMaterials_Chapter_Delete(System.Int64 Idx) {
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_TeachMaterials_Chapter_Delete");
                cmd.SetParameterValue("@Idx", Idx);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }
        }


        public static List<TS_TeachMaterials_Chapter_Entity> TS_TeachMaterials_Chapter_ListByMaterialsId(System.Int64 materialsId) {
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_TeachMaterials_Chapter_ListByMaterialsId");
                cmd.SetParameterValue("@MaterialsId", materialsId);
                return cmd.ExecuteEntityList<TS_TeachMaterials_Chapter_Entity>();
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }
            return new List<TS_TeachMaterials_Chapter_Entity>(0);
        }


        public static bool TS_TeachMaterials_Chapter_ExistsByMaterialsId(System.Int64 materialsId) {
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_TeachMaterials_Chapter_ExistsByMaterialsId");
                cmd.SetParameterValue("@MaterialsId", materialsId);
                return ((long)cmd.ExecuteScalar()) > 0;
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }
            return false;
        }


        public static List<TS_TeachMaterials_Chapter_Entity> TS_TeachMaterials_Chapter_ListByTeacherId(System.Int64 UserId) {
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_TeachMaterials_Chapter_ListByTeacherId");
                cmd.SetParameterValue("@UserId", UserId);
                return cmd.ExecuteEntityList<TS_TeachMaterials_Chapter_Entity>();
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }
            return new List<TS_TeachMaterials_Chapter_Entity>(0);
        }


        public static List<TS_TeachMaterials_Chapter_Entity> TS_TeachMaterials_Chapter_ListByStudentId(System.Int64 UserId) {
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_TeachMaterials_Chapter_ListByStudentId");
                cmd.SetParameterValue("@UserId", UserId);
                return cmd.ExecuteEntityList<TS_TeachMaterials_Chapter_Entity>();
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }
            return new List<TS_TeachMaterials_Chapter_Entity>(0);
        }



    }
}
